//获取数据
function lie(){
    $.ajax({
        url: "./php/che.php",
        success: function (dt) {
            console.log($('.lf>:nth-child(3)'))
            $('.lf>:nth-child(3)').html(`
            <span style="display: none;"><span>
            `)
            dt.forEach(function(val){
                $('.lfs').append(`
                <ul>
                <li><input type="checkbox" name="xuan1"></li>
                <li>${val.tu}</li>
                <li>${val.bt}</li>
                <li>￥<span>${val.jia}</span></li>
                <li><span>-</span><span>1</span><span>+</span></li>
                <li>￥<span>${val.jia}</span></li>
                <p><span style="display: none;">${val.id}</span><u>删除</u></p>
            </ul>
                `)
                jia()
            })
        },
        dataType: "json",
    });
}
lie()
//事件委托
var boy=document.body
var xuans=document.getElementsByName('xuan1')
var quan=document.querySelector('[name=quan]')
var ul1=document.querySelector('.lfs')
var som=document.querySelector('.ri>.ri-1>:nth-child(6)>:last-child')
var cpsom=document.querySelector('.ri>.ri-1>:nth-child(3)>:last-child')
boy.onclick=function(e){
    var e=e || window.event
    var dian=e.srcElement || e.target

    if(dian.name=='quan'){
        for(var i=0;i<xuans.length;i++){
            if(dian.checked==false){
                xuans[i].checked=false
            }else{
                xuans[i].checked=true
            }
        }
    }
    //单选
    if(dian.name=='xuan1'){
        a=0  //用来判断被选中的次数
        for(var i=0;i<xuans.length;i++){
            if(xuans[i].checked==true){
                a++
            }
            if(a==xuans.length){
                quan.checked=true
            }else{
                quan.checked=false
            }
        }
    }

    //加数量
    if(dian.innerHTML=='+'){
        var shu=dian.previousElementSibling.innerHTML
        shu++
        dian.previousElementSibling.innerHTML=shu
        var dan=dian.parentNode.previousElementSibling.lastElementChild.innerHTML
        var xiao=dian.parentNode.nextElementSibling.lastElementChild.innerHTML
        xiao=shu*dan
        dian.parentNode.nextElementSibling.lastElementChild.innerHTML=xiao
        jia()
    }
    //减数量
    if(dian.innerHTML=='-'){
        var shu=dian.nextElementSibling.innerHTML
        shu--
        if(shu<1){
            shu=1
        }
        dian.nextElementSibling.innerHTML=shu
        var dan=dian.parentNode.previousElementSibling.lastElementChild.innerHTML
        var xiao=dian.parentNode.nextElementSibling.lastElementChild.innerHTML
        xiao=shu*dan
        dian.parentNode.nextElementSibling.lastElementChild.innerHTML=xiao
        jia()

    }
    if(dian.innerHTML=='删除'){
        var id=dian.previousElementSibling.innerHTML
        console.log(id)
      $.ajax({
          url:"./php/del.php",
          type:"get",
          data:{
              id:id
          },
          success:function(dt){
               lie()
               console.log(456)
          },
          dataType:"json"
      })
      jia()
    }
}
function jia(){
    var uls=ul1.getElementsByTagName('ul')
    var ji=0
    for(var i=0;i<uls.length;i++){
        ji+=parseFloat(uls[i].children[5].lastElementChild.innerHTML)
    }
    som.innerHTML=ji
    cpsom.innerHTML=ji
    $('.lf>:nth-child(1) span').html(uls.length)
}
jia()

